<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Order extends CI_Controller 
{
	function __construct()
	{
		parent::__construct();
		
		//load helper
		$this->load->helper('login_helper');
		
			//load model

		//$this->load->model('Morder');
			$this->load->model('Morder');
	
    //load libraries

    $this->load->library('pagination');

    $this->load->library('form_validation');
	// session
	 session_start();
	if ( !isset($_SESSION['row_number']))

		$_SESSION['row_number']= 12;
	if ( !isset($_SESSION['month']))

		$_SESSION['month']= 5;
	if ( !isset($_SESSION['year']))

		$_SESSION['month']= 2011;
	
	// Check login
	check_login();
	load_lang("admin");

	}
	function printer()
	{
		$id = $this->uri->segment(4);
		if(is_numeric($id) && $id > 0 )
		{ 
			$data["result"] = $this->Morder->info_from_id($id,1);
			$data["order_detail"] = $this->Morder->info_from_id($id,2);
		
			$data["title"]= "Đơn đặt hàng";
			$this->load->view("admin/order_print",$data);
		}
	}
	//**************************** Product  ************************************************//
	function index($title="",$page='')
	{
//	echo $title;
$area = $this->session->userdata("area");
		$limit = $_SESSION['row_number'];
		 if (empty($page))	$page = 0; //default value
		 
		
				$config['base_url'] = base_url().'/admin/order/index/';
				$config['total_rows'] = $this->Morder->count_all(1);
				$config['uri_segment'] = 4;
				$data['result'] = $this->Morder->list_data($page,$limit);
					
		
	//	echo $config['total_rows'];
		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

	

		$config['num_links'] = 2;

		//$config['total_rows'] = $this->Morder->count_all(1);

		$config['per_page'] = $limit;
		

		$this->pagination->initialize($config);

   

   // $data['result'] = $this->Morder->list_data($page,$limit,1);// $this->Muser->user_review($page, $limit);
	//$data['result'] = $this->Morder->list_data_deliver($page,$limit);
    $data['title'] ='Đơn đặt hàng';

    $data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());

    $data['main'] = 'order_review';

    $this->load->view('admin/layout/template_main', $data);
	
	
	
	}

	function update()
	{	
		$id = $this->uri->segment(4);
		$data["result"] = $this->Morder->info_from_id($id,1);
		if(isset($_POST['update']))
		{
			$data1 = array(
				"order_status"=> $this->input->post('status'),
				"order_modify"=> time(),
			);		
			///$email  = $data['result'][''];	
			if($this->Morder->update($data1,$id))
				redirect("admin/order/index/undelivered","refesh");
			
				
		}
		else if(isset($_POST["cancel"]))
		{
			//redirect("admin/order/index/","refesh");
			
			redirect('admin/order/index/', 'refresh');
	
			
		}
		
		$data["order_detail"] = $this->Morder->info_from_id($id,2);
	
		$data["main"]= "order_update";
		$data["title"]= "Cập nhật đơn đặt hàng";
		$this->load->View("admin/layout/template_main",$data);
	}
	
	function request()
	{//determine 'action'
		$require = array('show', 'delete','undelete');

		foreach ($_POST as $key=>$value)

		{
			if (in_array($key, $require))
			{
				$act = $key;
	
			}

		}
	//process 'action'
//	echo $act." ";
		if (! empty($act))

		{
				$referer = $_SERVER['HTTP_REFERER'];
	//echo $referer;
	    
			switch ($act)

			{

				case 'show': //show

				{
				
					$_SESSION['row_number'] = $this->input->post('row_number');
					
				} break;



				case 'delete': //delete

				{

					$this->Morder->delete();
//					redirect('admin/order/', 'refresh');

				} break;
				case 'undelete': //delete

				{

					$this->Morder->undelete();
					

				} break;

			}//end switch



		if (!$referer == '' ) {
	
		 redirect(  $referer, 'refresh');
	
	   }
	   else
	   	redirect('admin/order/index/', 'refresh');

		}//end if

	}
	

	
	
	
public function delete($id)
	{
		if($id >0 )
		
			$this->Morder->del_item($id);
		redirect('admin/order/', 'refresh');
		
	}
	

}








?>
